/**
 * The contents of this file are subject to the license and copyright detailed in the LICENSE and NOTICE files at the root of the source tree and available online at
 * http://www.askosi.org/license.html
 */
package be.destin.skos.core;

import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class EnumSource {

	/** log4j category */
	private static Logger log = Logger.getLogger(EnumSource.class);
	
	protected static void load(ConceptScheme aScheme, Properties properties, String configFile, Map<String, String> notationsDefinitions) {
		String className = properties.getProperty("class");
		if (className==null || className.isEmpty()) {
			log.error(aScheme.getAbout() + ": class=package.class-name must be specified in "+configFile);						
		} else {
			Class enumClass;
			try {
				enumClass = Class.forName(className);
				if (enumClass.isEnum()) {
					for (Object oneEnumObject: enumClass.getEnumConstants()) {
						String currCode = ((java.lang.Enum) oneEnumObject).name(); 
						Concept entry = aScheme.findAbout_Concept(currCode);
						if (entry== null) {
							entry = Concept.ensure(aScheme,currCode);
							entry = aScheme.putConcept(currCode, entry);
							entry.putPrefLabel(TermList.anyLanguage, null, ((java.lang.Enum) oneEnumObject).toString(), null, null);
							entry.putUrl(TermList.anyLanguage, className+'.'+currCode);
						}
					}
				}
			} catch (ClassNotFoundException cnfe) {
				log.error(aScheme.getAbout() + ": Class " + className + " does not exist.");			
			}
		}
		if (Level.DEBUG.equals(log.getEffectiveLevel())) {
			StringBuffer result=new StringBuffer();
			for (Concept aConcept: aScheme.getConcept()) {
				result.append(aConcept.toString());
				result.append(" -- ");
			}
			log.debug(result.toString());
		}
	}
}
